package com.projectCommon.util.threadSave;



import com.projectCommon.bean.cache.EventFlag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/07/16/20:56
 * @target 心若有所向往，何惧道阻且长
 */
public class ThreadLogHold {
    //再维护一个  list 用于记录每次开始结束。
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    public static void init() {
        HashMap<String, Object> threadMap = new HashMap<>();
        threadMap.put("eventStartCount", 0);
        threadMap.put("eventEndCount", 0);
        //感觉这个list  就把事情办了。！！！
        threadMap.put("eventFlags", new ArrayList<EventFlag>());
        //计数器
        threadMap.put("counter", 0);
        threadLocal.set(threadMap);
    }

    public static void eventStartCountAdd() {
        Map<String, Object> threadMap = threadLocal.get();
        Integer eventStartCount = getEventStartCount();
        eventStartCount++;
        threadMap.put("eventStartCount", eventStartCount);
    }

    public static void eventEndCountAdd() {
        Map<String, Object> threadMap = threadLocal.get();
        Integer eventEndCount = getEventEndCount();
        eventEndCount++;
        threadMap.put("eventEndCount", eventEndCount);
    }

    public static Integer getEventStartCount() {
        Map<String, Object> threadMap = threadLocal.get();
        return (Integer) threadMap.get("eventStartCount");
    }

    public static Integer getEventEndCount() {
        Map<String, Object> threadMap = threadLocal.get();
        return (Integer) threadMap.get("eventEndCount");
    }

    public static boolean addEventFlag(EventFlag eventFlag) {
        Map<String, Object> threadMap = threadLocal.get();
        List<EventFlag> eventFlags = (List<EventFlag>) threadMap.get("eventFlags");
        boolean add = eventFlags.add(eventFlag);
        return add;
    }

    //得到一个计数器开始结束的次数，每次得到都会+1，因为是单线程的操作，所以不需要考虑县城安全的问题
    public static int getCounter() {
        Map<String, Object> threadMap = threadLocal.get();
        Integer counter = (Integer) threadMap.get("counter");
        counter++;
        threadMap.put("counter", counter);
        return counter;
    }

    public static List<EventFlag> getEventFlags() {
        Map<String, Object> threadMap = threadLocal.get();
        List<EventFlag> eventFlags = (List<EventFlag>) threadMap.get("eventFlags");
        return eventFlags;
    }

    public static void remove() {
        threadLocal.remove();
    }
}
